package com.example.duomeiti;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class CameraActivity extends AppCompatActivity {
    Button btnPohto;
    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        btnPohto = findViewById(R.id.btnPhoto);
        img = findViewById(R.id.img);
        //设置监听
        btnPohto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //使用Intent调用系统里面的相机进行拍照
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0406);
            }
        });
    }
    //接受相机返回的图像

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //三个参数 请求码，结果码，数据
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0406) {
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            img.setImageBitmap(bitmap);
        }
    }
}
